package cn.com.shopec.erp.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.base.dao.IPurchaserOrder;
import cn.com.shopec.erp.base.service.IPurchaseOrderTypeService;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.financing.entity.PurchaserOrder;
import cn.com.shopec.erp.financing.entity.PurchaserOrderVo;
/**
 * 采购单类型有效期设置
 * 
 * */
@Service(value="PurchaserOrcerService")
public class PurchaserOrcerServiceImp implements IPurchaseOrderTypeService
{
     @Resource
     private IPurchaserOrder iPurchaserOrder;
     
     private static Log log = LogFactory.getLog(PurchaserOrcerServiceImp.class);

     @Override
     public PurchaserOrder getType(String account)
     {
          
          
          PurchaserOrder po = null;
   
          DetachedCriteria dc = iPurchaserOrder.createCriteria();
          
          dc.add(Restrictions.eq("purchaseType", Integer.valueOf(account)));
              
          dc.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
          List<PurchaserOrder> lios = iPurchaserOrder.findByCriteria(dc);
          
          for(int i=0;null!=lios&&i<lios.size();i++){
                 po=new PurchaserOrder();
                  po = lios.get(0);
          }
        
          
         return po;  
          
          
        
     }
     
     
     public PageFinder<PurchaserOrderVo> getPurchaser(PurchaserOrderVo vo, PageQuery pageQuery)
     {
          
          DetachedCriteria criteria = iPurchaserOrder.createCriteria();
          criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
          PageFinder<PurchaserOrder> pageFinder=iPurchaserOrder.pagedByCriteria(criteria, pageQuery.getPage(), pageQuery.getPageSize());
          List<PurchaserOrderVo> asVoList = new ArrayList<PurchaserOrderVo>();          
          for (PurchaserOrder as : pageFinder.getData()) {
               asVoList.add(getPurchaserOrderVo(as));
              }
          PageFinder<PurchaserOrderVo> asVoPageFinder = new PageFinder<PurchaserOrderVo>(pageFinder.getPageNo(), pageFinder.getPageSize(),pageFinder.getRowCount());
          asVoPageFinder.setData(asVoList);
          return asVoPageFinder;
          
          
     }
     
     /**
      * @methodName：getPurchaserOrderVo
      * @Description: 采购单设置实体表转换虚拟表对象
      * @param PurchaserOrder
      * @return PurchaserOrder
      * @author: 
      * @date: 2013-5-26 下午18:22:03
      */
     
     public PurchaserOrderVo getPurchaserOrderVo(PurchaserOrder po){
          PurchaserOrderVo  vo=new PurchaserOrderVo();
          vo.setCode(po.getCode());
          vo.setDeleteFlag(po.getDeleteFlag());
          vo.setId(po.getId());
          vo.setPeriodValidity(po.getPeriodValidity());
          vo.setPurchaseType(po.getPurchaseType());         
          return vo;
     }
     
     
     

     /**
      * 采购单类型保存
      * */
     @Transactional
     public void savePurchaseType(PurchaserOrder po)
     {
          try
          {
               iPurchaserOrder.save(po);
          }
          catch (Exception e)
          {
               log.error(e.getMessage(), e);
          }
     }


	@Override
	public PurchaserOrder getPurchaserOrderById(String id) {
		return iPurchaserOrder.findUniqueBy("id", id);
	}


	@Override
	@Transactional
	public void getByIdUpdateReceipts(PurchaserOrder purchaserOrder) throws Exception {
		if(null != purchaserOrder)
		{
			PurchaserOrder order=	getPurchaserOrderById(purchaserOrder.getId());
			order.setPeriodValidity(purchaserOrder.getPeriodValidity());
			order.setCode(purchaserOrder.getCode());
			order.setPurchaseType(purchaserOrder.getPurchaseType());
			order.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
			iPurchaserOrder.update(order);
		}
	}
     
     
}
